package com.atguigu.flink07;

import com.atguigu.beans.WaterSensor;
import com.atguigu.func.WaterSensorMapFunction;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @author Felix
 * @date 2024/2/26
 * 该案例演示了Flink的定时器
 *      只能在keyedStream下使用定时任务
 *
 */
public class Flink04_timer {
    public static void main(String[] args) throws Exception {
        //TODO 1.基本环境准备
        //1.1 指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.2 设置并行度
        env.setParallelism(1);
        //TODO 2.从指定的网络端口读取数据
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);
        //TODO 3.对读取的数据进行类型转换   String ->WaterSensor
        SingleOutputStreamOperator<WaterSensor> wsDS = socketDS.map(new WaterSensorMapFunction());
        //TODO 4.指定Watermark以及提取事件时间字段
        SingleOutputStreamOperator<WaterSensor> withWatermarkDS = wsDS.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<WaterSensor>forMonotonousTimestamps()
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<WaterSensor>() {
                                    @Override
                                    public long extractTimestamp(WaterSensor ws, long recordTimestamp) {
                                        return ws.ts;
                                    }
                                }
                        )
        );
        wsDS.process(
                new ProcessFunction<WaterSensor, String>() {
                    @Override
                    public void processElement(WaterSensor value, ProcessFunction<WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {
                        ctx.timerService().registerProcessingTimeTimer(ctx.timerService().currentProcessingTime() + 10000L);
                    }

                    @Override
                    public void onTimer(long timestamp, ProcessFunction<WaterSensor, String>.OnTimerContext ctx, Collector<String> out) throws Exception {
                        System.out.println("定时器被执行了");
                    }
                }
        ).print();
        /*//TODO 5.按照传感器的id进行分组
        KeyedStream<WaterSensor, String> keyedDS = withWatermarkDS.keyBy(WaterSensor::getId);
        //TODO 6.注册定时任务
        SingleOutputStreamOperator<String> processDS = keyedDS.process(
                new KeyedProcessFunction<String, WaterSensor, String>() {
                    //每来一条元素被调用一次
                    @Override
                    public void processElement(WaterSensor ws, KeyedProcessFunction<String, WaterSensor, String>.Context ctx, Collector<String> out) throws Exception {
                        //当前元素对应的事件时间
                        Long timestamp = ctx.timestamp();
                        System.out.println("timestamp:" + timestamp);
                        //获取当前分组的key
                        String currentKey = ctx.getCurrentKey();
                        System.out.println("currentKey:" + currentKey);
                        //如果要使用侧输出流，那么必须使用底层的process进行处理，才能够将数据写到侧输出流
                        //ctx.output();
                        //获取定时服务
                        TimerService timerService = ctx.timerService();

                        //注册处理时间定时器
                        long currentProcessingTime = timerService.currentProcessingTime();
                        timerService.registerProcessingTimeTimer(currentProcessingTime + 10000L);

                        //注册事件时间定时器----通过Watermark进行触发
                        //timerService.registerEventTimeTimer(10);

                        //获取当前水位线
                        long currentWatermark = timerService.currentWatermark();
                        System.out.println("currentWatermark:" + currentWatermark);

                        //获取当前处理时间
                        //timerService.currentProcessingTime()

                        //删除事件时间定时器
                        //timerService.deleteEventTimeTimer();

                        //删除处理时间定时器
                        //timerService.deleteProcessingTimeTimer();

                    }

                    //定时器触发的时候 被执行

                    *//**
                     *
                     * @param timestamp 定时器被触发的时间
                     * @param ctx 上下文对象
                     *//*
                    @Override
                    public void onTimer(long timestamp, KeyedProcessFunction<String, WaterSensor, String>.OnTimerContext ctx, Collector<String> out) throws Exception {
                        String currentKey = ctx.getCurrentKey();
                        System.out.println("key=" + currentKey + "现在时间是" + timestamp + "定时器触发");
                    }
                }
        );*/
        //TODO 7.输出
        //processDS.print("$$$$");

        //TODO 8.提交
        env.execute();
    }
}
